<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <!--              <j-search-select-tag v-model="model.customerId" dict="biz_customer,name,id"  />-->
              <!--              <JSelectUserByDep></JSelectUserByDep>-->
              <JSelectMultiUser v-model="model.customerId" :query-config="selectUserQueryConfig" :url="kehuurl"
                                @change="kehuChange" :disabled="isdisable"/>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">-->
<!--              <j-search-select-tag v-model="model.customerId" dict="biz_customer,name,id"  />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->

<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsIds">-->
<!--              <j-multi-select-tag type="list_multi" v-model="model.contactsIds" dictCode="biz_contacts,name,id" placeholder="请选择联系人" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsIds">
              <!--              <j-multi-select-tag type="list_multi" v-model="model.contactsIds" dictCode="biz_contacts,name,id" placeholder="请选择联系人" />-->
              <lianxiren v-model="model.contactsIds" :query-config="lianxirenConfig" :multiple="true"
                         :url="lianxirenUrl" v-if="customerIdShow" @change="jihuiChange"></lianxiren>
              <span v-else>无</span>
              <!--              <LIANXIREN v-if="model.customerId" v-model="model.contactsIds" ref="lianxiren" :customerId="model.customerId"></LIANXIREN>-->
              <!--              <span v-else>无</span>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跟进方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mode">
              <j-dict-select-tag type="list" v-model="model.mode" dictCode="follow_mode" placeholder="请选择跟进方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跟进业务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="follow_type" placeholder="请选择跟进业务类型" @change="choseBusinessId"/>
            </a-form-model-item>
          </a-col>

<!--          销售机会-->
          <a-col :span="24" v-if="model.type=='1'">
            <a-form-model-item label="业务类型单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <!--              <j-dict-select-tag type="list" v-model="model.businessId" />-->
              <a-select v-model="model.businessId"  placeholder="请选择单号" @change="chosedanhao">
                <!--                <a-select-option :value="undefined">请选择</a-select-option>-->
                <a-select-option v-for="(item, key) in businessIdList" :key="key" v-model="item.id">
      <span style="display: inline-block;width: 100%" >
        {{ item.number || item.label }}
      </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

<!--          业务类型-->
          <a-col :span="24" v-if="model.type=='2'">
            <a-form-model-item label="业务类型单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <!--              <j-dict-select-tag type="list" v-model="model.businessId" />-->
              <a-select v-model="model.businessId"  placeholder="请选择单号" @change="chosedanhao">
                <!--                <a-select-option :value="undefined">请选择</a-select-option>-->
                <a-select-option v-for="(item, key) in hetongList" :key="key" v-model="item.id">
      <span style="display: inline-block;width: 100%" >
        {{ item.number || item.label }}
      </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

<!--          报价单-->
          <a-col :span="24" v-if="model.type=='3'">
            <a-form-model-item label="业务类型单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
<!--              <j-dict-select-tag type="list" v-model="model.businessId" />-->
              <a-select v-model="model.businessId"  placeholder="请选择单号"  @change="chosedanhao">
<!--                <a-select-option :value="undefined">请选择</a-select-option>-->
                <a-select-option v-for="(item, key) in baojiaList" :key="key" v-model="item.id">
      <span style="display: inline-block;width: 100%" >
        {{ item.number || item.label }}
      </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24"v-if="this.model.type=='2'">
            <a-form-model-item label="合同订单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessId">
              <j-dict-select-tag type="list" v-model="model.businessId"  placeholder="请选择合同订单" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="this.model.type=='3'">
            <a-form-model-item label="报价单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessId">
              <j-dict-select-tag type="list" v-model="model.businessId"  placeholder="请选择销报价单" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="跟进内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contents">
              <a-textarea v-model="model.contents" placeholder="请输入跟进内容" :rows="4"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="跟进时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="updateTime"
            >
              <a-date-picker
                style="width: 100%"
                placeholder="请选择跟进时间"
                :show-time="true"
                v-model="model.updateTime"
                format="YYYY-MM-DD HH:mm:ss"
                @change="onChange"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
             <j-area-linkage type="cascader" v-model="model.address" placeholder="请输入省市区"  @change="isAdress=true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addressInfo" v-if="isAdress">
              <a-input v-model="model.addressInfo" placeholder="请输入详细地址"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUserCustom'
  import lianxiren from '@/components/jeecgbiz/lianxirenCustom'
  import xiaoshoujihui from '@/components/jeecgbiz/xiaoshoujihui/index'
  import moment from 'moment'

  export default {
    name: 'FollowForm',
    components: {
      JSelectMultiUser,
      lianxiren,
      xiaoshoujihui,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        isAdress:false,
        isdisable:false,
        kehuurl: '/customer/customer/list?filed=2&id=',
        // 选择用户查询条件配置
        selectUserQueryConfig: [
          { key: 'phone', label: '电话' }
        ],
        customerIdShow: false,
        //联系人
        lianxirenUrl: '/sys/common/queryCustomerId?customerId=',
        lianxirenConfig: [
          { key: 'name', label: '姓名' }
          /* {key: 'leixing', label: '类型'},
          {key: 'tel', label: '电话'} */

        ],
        /* kehuurl:'/customer/customer/list',
        // 选择用户查询条件配置
        selectUserQueryConfig: [
          {key: 'phone', label: '电话'},
        ], */
        businessIdList:[],//销售机会下拉列表
        hetongList:[],//合同订单下拉列表
        baojiaList:[],//报价单下拉列表
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           customerId: [
              { required: true, message: '请输入客户名称!'},
           ],
           mode: [
              { required: true, message: '请选择跟进方式(1.当面拜访 2.电话拜访  3.网络拜访)!'},
           ],
           // type: [
           //    { required: true, message: '请选择跟进业务类型(1.销售机会 2.合同订单 3.报价单)!'},
           // ],
           businessId: [
              { required: true, message: '请选择!'},
           ],
          contents: [
            { required: true, message: '请填写内容!'},
          ],
          addressInfo: [
            { required: true, message: '请填写详细地址!'},
          ],
        },
        url: {
          add: "/follow/follow/add",
          edit: "/follow/follow/edit",
          queryById: "/follow/follow/queryById",
          queryByCode:"/follow/follow/queryByCode",//销售机会
          businessNumber:"/follow/follow/queryBusinessId"//业务类型单号
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch: {
      'model.customerId': {
        deep: true,
        handler(newValue, oldValue) {
          console.log('model.customerID变化')

          if (!newValue || newValue !== oldValue) {
            this.customerIdShow = false
          }
          console.log('newValue', newValue)
          console.log('oldValue', oldValue)
          if (newValue) {
            this.$nextTick(() => {
              this.customerIdShow = true
              this.kehuurl = '/customer/customer/list?filed=2&id=' + newValue
              console.log('执行新的kehuurl', this.kehuurl)
              if (this.model.opportunityId) {
                this.lianxirenUrl = '/sys/common/queryCustomerId?customerId=' + newValue + '&id=' + this.model.contactsIds
              } else {
                this.lianxirenUrl = '/sys/common/queryCustomerId?customerId=' + newValue
              }
            })

          }
          /*  if (this.model.opportunityId) {
             this.xiaoshoujihuiUrl = '/sys/common/querySalesOpportuninty?customerId=' + newValue + '&id=' + this.model.opportunityId
             this.lianxirenUrl = '/sys/common/queryCustomerId?customerId=' + newValue + '&id=' + this.model.contactsIds
           } else {
             this.xiaoshoujihuiUrl = '/sys/common/querySalesOpportuninty?customerId=' + newValue
             this.lianxirenUrl = '/sys/common/queryCustomerId?customerId=' + newValue
           } */

          /* if (!newValue) {
            this.model.opportunityId = ''
            this.model.contactsIds = ''

          } */
        }
      }
    },
    methods: {
      jihuiChange(e){

      },
      //执行时间不可选今日之前
      // disabledDate(current) {
      //   return current && current < moment().subtract(1, 'days').endOf('day')
      // },
      //访客计划完成时，新增跟进记录，让客户不能编辑
      changedisable(){
        this.isdisable = true
      },
      //跟进时间
      onChange(value, dateString) {
        console.log('Selected Time: ', value);
        console.log('Formatted Selected Time: ', dateString);
        this.model.updateTime = dateString
      },
      kehuChange(e) {
        console.log(e)
        this.customerIdShow = true
        // this.model.opportunityId = ''
        // this.model.contactsIds = ''
        /* this.xiaoshoujihuiUrl = '/sys/common/querySalesOpportuninty?customerId=' + e
        this.lianxirenUrl = '/sys/common/queryCustomerId?customerId=' + e */
      },
      chosedanhao(e){
        console.log('选择单号',e)
        getAction(this.url.queryByCode,{code:this.model.type,customerId:this.model.customerId}).then(res=>{
          console.log('跟进业务类型返回数据',res)
          this.businessIdList = res.result.salesOpportunityList
          this.hetongList = res.result.contractOrderList
          this.baojiaList = res.result.quotationList
        })
      },

      choseBusinessId(e){
        console.log('跟进业务类型',this.model.type)
        this.model.businessId=""
        getAction(this.url.queryByCode,{code:this.model.type,customerId:this.model.customerId}).then(res=>{
          console.log('跟进业务类型返回数据',res)
          this.businessIdList = res.result.salesOpportunityList
          this.hetongList = res.result.contractOrderList
          this.baojiaList = res.result.quotationList
        })
      },
      // 编辑页面获取业务类型单号
      getBusinessNumber(){
        getAction(this.url.businessNumber,{code:this.model.type,businessId:this.model.businessId}).then(res=>{
          console.log('业务类型单号',res)
          this.model.businessId = res.result.number

        })
      },
      //销售机会change事件
      // businessIdChange(e){
      //    console.log('选中销售机会',e)
      //   this.model.businessId=""
      // },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.getBusinessNumber();
        this.visible = true;
      },
      submitForm () {
        console.log(this.model)
        // debugger
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>